package etcd

import (
	"fmt"
	"strconv"
	"time"
)

func checkError(err error) {
	if err != nil {
		panic(err)
	}
}

func TestValue() {
	err := Put("test1", "v1")
	checkError(err)

	v, err := Get("test1")
	fmt.Println(v)

	v, err = Get("test2")
	fmt.Println(v)

	err = Put("test2", "v2")
	checkError(err)
	v, err = Get("test2")
	fmt.Println(v)
}

func TestGetPrefix() {
	prefix := "test"
	m, err := GetPrefix(prefix)

	checkError(err)
	fmt.Println(m)
}

func TestWatch() {
	key := "test3"

	for i := 0; i < 3; i++ {
		var value = i
		go func() {
			Watch1(key, "client"+strconv.Itoa(value))
		}()
	}

	err := Put(key, "v3")
	checkError(err)

	err = Delete(key)
	checkError(err)
}

func TestLease() {
	id, err := CreateLease(3 * time.Minute)
	checkError(err)

	err = PutWithLease("l1", "1", id)
	checkError(err)

	err = PutWithLease("l2", "2", id)
	checkError(err)

	ch, err := KeepAlive(id)
	checkError(err)
	//永久阻塞，保持心跳
	for {
		fmt.Println(<-ch)
	}
}
